<template>
    <div>
        <el-form :model="addStudent">
              <el-form-item label="用户名" :label-width="formLabelWidth">
               <el-input v-model="addStudent.userName" ></el-input>
              </el-form-item>
              <el-form-item label="密码" :label-width="formLabelWidth">
               <el-input v-model="addStudent.password" ></el-input>
              </el-form-item>
              <el-form-item label="真实姓名" :label-width="formLabelWidth">
               <el-input v-model="addStudent.realName" ></el-input>
              </el-form-item>
              <el-form-item label="性别" :label-width="formLabelWidth">
               <el-select
                 v-model="addStudent.sex"

                 filterable
                 placeholder="请选择"
               >
                 <el-option
                   v-for="item in sex"
                   :key="item.dictValue"
                   :label="item.dictLabel"
                   :value="item.dictValue"
                 >
                 </el-option>
               </el-select>
              </el-form-item>
              <el-form-item label="状态" :label-width="formLabelWidth">
               <el-select
                 v-model="addStudent.status"
                 filterable
                 placeholder="请选择"
               >
                 <el-option
                   v-for="item in status"
                   :key="item.dictValue"
                   :label="item.dictLabel"
                   :value="item.dictValue"
                 >
                 </el-option>
               </el-select>
              </el-form-item>
              <el-form-item label="年级" :label-width="formLabelWidth">
               <el-select
                 v-model="addStudent.grade"
                 filterable
                 placeholder="请选择"
               >
                 <el-option
                   v-for="item in grade"
                   :key="item.dictValue"
                   :label="item.dictLabel"
                   :value="item.dictValue"
                 >
                 </el-option>
               </el-select>
              </el-form-item>

              <el-form-item label="年龄" :label-width="formLabelWidth">
               <el-input v-model="addStudent.age" ></el-input>
              </el-form-item>
              <el-form-item label="手机" :label-width="formLabelWidth">
               <el-input v-model="addStudent.phone" ></el-input>
              </el-form-item>
              <el-form-item   label="出生日期" :label-width="formLabelWidth">
                <div class="block">
              
                  <el-date-picker
             
                    v-model="addStudent.birthday"
                    type="date"
                    placeholder="选择日期" value-format="yyyy-MM-dd HH:ss:mm"
                    >
                  </el-date-picker>
                </div>
              </el-form-item>
              <el-form-item  :label-width="formLabelWidth">
                    <el-button type="primary" v-if="this.id == null" @click="add()">提交</el-button>
                    <el-button type="primary" v-else @click="update()">修改</el-button>
                    <el-button>重置</el-button>
              </el-form-item>
              

        </el-form>
        
    </div>
</template>
<script>
import axios from 'axios'
export default {
    data(){
        return {
            formLabelWidth:"120px",
            id:null,
            sex:[],
            status:[],
            grade:[],
            addStudent:{
               "age": "",
               "birthday": "",
               "createTime": "",
               "id": 0,
               "modifyTime": "",
               "password": "",
               "phone": "",
               "realName": "",
               "sex": "",
               "status": "",
               "userName": "",
               "grade":""
           }
        }
    },
    created(){
        this.findType()
        this.id = sessionStorage.getItem("id");
        console.log(this.id);
        if (sessionStorage.getItem("id")) {
            this.getAdmin()
        }
    },
    methods:{
        findType(){
            axios.post("/api/sysdict/findSysDictBySex?type=sys_user_sex").then(res=>{
                this.sex = res.data.data
            })
            axios.post("/api/sysdict/findSysDictBySex?type=sys_grade_status").then(res=>{
                this.grade = res.data.data
            })
            axios.post("/api/sysdict/findSysDictBySex?type=sys_normal_disable").then(res=>{
                this.status = res.data.data
            })
        },
        add(){
            axios.post("/api/student/addStudent",this.addStudent).then(res=>{
                alert(res.data.message)
                if(res.data.message == '新增成功'){
                    this.$router.push('/TbAdminShow')
                }
            })
        },
        getAdmin(){
            axios.get("/api/student/getStudentById?id="+this.id).then(res=>{
                this.addStudent = res.data.data
                sessionStorage.removeItem("id")
            })
        },
        update(){
            axios.post("/api/student/upStudent",this.addStudent).then(res=>{
                if(res.data.code == 200){
                    alert(res.data.message)
                    this.$router.push('/TbAdminShow')
                }
            })
        }
    }
}
</script>